package com.example.qu_tao.entity;
import android.os.Parcel;
import android.os.Parcelable;
import java.util.Objects;
import java.util.UUID;
public class Address implements Parcelable {
    public String id;         // 唯一标识符
    public String receiver;   // 收货人
    public String phone;      // 手机号码
    public String region;     // 地区
    public String detailAddress; // 详细地址
    public boolean isDefault; // 是否为默认地址

    // 构造函数（新增地址时使用）
    public Address() {
        this.id = UUID.randomUUID().toString(); // 自动生成UUID
    }

    // 完整构造函数（用于编辑地址）
    public Address(String id, String receiver, String phone, String region, String detailAddress, boolean isDefault) {
        this.id = id;
        this.receiver = receiver;
        this.phone = phone;
        this.region = region;
        this.detailAddress = detailAddress;
        this.isDefault = isDefault;
    }

    // 从Parcel读取数据
    protected Address(Parcel in) {
        id = in.readString();
        receiver = in.readString();
        phone = in.readString();
        region = in.readString();
        detailAddress = in.readString();
        isDefault = in.readByte() != 0;
    }

    public static final Creator<Address> CREATOR = new Creator<Address>() {
        @Override
        public Address createFromParcel(Parcel in) {
            return new Address(in);
        }
        @Override
        public Address[] newArray(int size) {
            return new Address[size];
        }
    };

    // Getter和Setter
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getReceiver() {
        return receiver;
    }

    public void setReceiver(String receiver) {
        this.receiver = receiver;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getRegion() {
        return region;
    }

    public void setRegion(String region) {
        this.region = region;
    }

    public String getDetailAddress() {
        return detailAddress;
    }

    public void setDetailAddress(String detailAddress) {
        this.detailAddress = detailAddress;
    }

    public boolean isDefault() {
        return isDefault;
    }

    public void setDefault(boolean defaultAddress) {
        isDefault = defaultAddress;
    }

    // 重写equals和hashCode（基于ID）
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Address address = (Address) o;
        return Objects.equals(id, address.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(id);
        dest.writeString(receiver);
        dest.writeString(phone);
        dest.writeString(region);
        dest.writeString(detailAddress);
        dest.writeByte((byte) (isDefault ? 1 : 0));
    }
}